﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawTrapezoids : MonoBehaviour {
	public float lineWidth = 0.01f;//线宽
	public Color lineColor;//线的颜色
	public List<Vector2> pointList = new List<Vector2>(); //点集

	public Shader shader;//线的材质
	private MeshFilter meshFilter;//网格过滤器
	private MeshRenderer meshRenderer;//网格渲染器
	private Vector3[] vertices;//网格点集
	private int[] triangles;//需要渲染的三角面
	void Start() {
		meshFilter = GetComponent<MeshFilter>();
		meshRenderer = GetComponent<MeshRenderer>();
		meshRenderer.material.color = lineColor;
		meshRenderer.material.shader = shader;
	}

	private void Update() {
		//UpdateLine();
	}

	public void UpdateLine() {

		int len = pointList.Count;
		//pointList => points
		vertices = new Vector3[len * 4];
		for (int i = 0; i < len; i++) {
			vertices[len * 0 + i] = pointList[i];
			vertices[len * 1 + i] = new Vector3(pointList[i].x, 0);
			vertices[len * 2 + i] = new Vector3(pointList[i].x + lineWidth, pointList[i].y);
			vertices[len * 3 + i] = new Vector3(pointList[i].x + lineWidth, 0);

		}
		//points => triangles
		triangles = new int[len * 3 * 4];
		for (int i = 0, j = 0; i < len - 1; i++) {

			triangles[j++] = i + len * 0;
			triangles[j++] = i + len * 2;
			triangles[j++] = i + len * 3;

			triangles[j++] = i + len * 0;
			triangles[j++] = i + len * 3;
			triangles[j++] = i + len * 2;

			triangles[j++] = i + len * 0;
			triangles[j++] = i + len * 3;
			triangles[j++] = i + len * 1;

			triangles[j++] = i + len * 0;
			triangles[j++] = i + len * 1;
			triangles[j++] = i + len * 3;

		}

		Mesh mesh = meshFilter.mesh;
		mesh.vertices = vertices;
		mesh.triangles = triangles;
		meshFilter.mesh = mesh;
	}

}
